package cn.com.open.itembank.controller;

import cn.com.open.itembank.common.GlobalConst;
import cn.com.open.itembank.dto.CopyCourseModel;
import cn.com.open.itembank.dto.ReturnModel;
import cn.com.open.itembank.entity.CourseItemBank;
import cn.com.open.itembank.interceptor.Log;
import cn.com.open.itembank.service.CourseRemoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by lucky on 2017/7/13.
 */

/**
 * 课程控制器
 */
@RestController
@RequestMapping("/course")
public class CourseController extends BaseController {
    @Autowired
    private CourseRemoteService service;

    //region 新增相关（Create：理论方法 HttpPost）

    //region 复制课程

    /**
     * 复制课程
     *
     * @param request        the request
     * @param response       the response
     * @param sourceCourseId 源资源课程ID（被共享的课程）
     * @param model          复制课程模型
     */
    @Log(optType = "复制课程", optDescription = "复制课程，课程ID：%s", keyName = "sourceCourseId")
    @RequestMapping(value = "/{courseId}/copy", method = RequestMethod.POST)
    public void copyCourse(HttpServletRequest request, HttpServletResponse response,
                           @PathVariable(value = "courseId") String sourceCourseId,
                           @RequestBody CopyCourseModel model) {

        ReturnModel result = service.copy(sourceCourseId, model);

        if (result.getCode() == GlobalConst.COURSE_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (result.getCode() == GlobalConst.COURSE_EXIST) {
            response.setStatus(HttpServletResponse.SC_CONFLICT);
        } else {
            responseJson(request, response, result.getData(), result.getMessage(), result.getCode());
        }
    }

    //endregion

    //endregion

    //region 删除相关（Delete：理论方法 HttpDelete）

    //endregion

    //region 更新相关（Update：理论方法 HttpPut）

    //endregion

    //region 查询相关（Retrieve：理论方法 HttpGet）

    //region 获取题库信息

    /**
     * 获取题库信息
     *
     * @param request  the request
     * @param response the response
     * @param courseId 资源课程id
     */
    @Log(optType = "获取题库信息", optDescription = "获取题库信息，课程ID：%s", keyName = "id")
    @RequestMapping(value = "/{courseId}", method = RequestMethod.GET)
    public void getCourse(HttpServletRequest request, HttpServletResponse response,
                          @PathVariable(value = "courseId") String courseId) {

        if (!isPermissionForCourse(request, courseId)) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }

        CourseItemBank model = service.getCourseItemBankInfoByCourseId(courseId);
        if (model != null) {
            responseJson(request, response, model, "");
        } else {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        }
    }

    //endregion

    //region Redis 查询题数集合,返回课程和题数

    /**
     * Redis 查询题数集合,返回课程和题数
     *
     * @param request  the request
     * @param response the response
     */
    @Log(optType = "获取课程和题数信息", optDescription = "Redis 查询题数集合,返回课程和题数", keyName = "courseQuestionCount")
    @RequestMapping(value = "/findcoursequestioncount", method = RequestMethod.GET)
    public void findCourseQuestionCount(HttpServletRequest request, HttpServletResponse response) {

        ReturnModel result = service.findCourseQuestionCount();
        if (result.getCode() == GlobalConst.COURSE_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (result.getCode() == GlobalConst.COURSE_EXIST) {
            response.setStatus(HttpServletResponse.SC_CONFLICT);
        } else {
            responseJson(request, response, result.getData(), result.getMessage(), result.getCode());
        }
    }

    //endregion

    //endregion
}
